#include "localconfig.h"

LocalConfig *LocalConfig::m_instance = nullptr;
QMutex LocalConfig::m_mutex;

LocalConfig *LocalConfig::getInstance(QObject *parent)
{
    if (!m_instance) {
        QMutexLocker locker(&m_mutex);
        if (!m_instance) {
            m_instance = new LocalConfig(parent);
        }
    }

    return m_instance;
}

void LocalConfig::init_Data()
{
    // qDebug()<<"Cpp:初始化函数触发!!";
    QString currentDir = QCoreApplication::applicationDirPath();
    // qDebug()<<"当前程序位置: "<<currentDir;
    QString fileName = QString("%1/config.ini").arg(currentDir);

    QSettings configIni(fileName, QSettings::IniFormat);

    configIni.beginGroup("PLC_Parameters");
    setPlc_Port(configIni.value("PLC_Port").toString());
    setPlc_Baudrate(configIni.value("PLC_BaudRate").toString());
    setPlc_Databit(configIni.value("PLC_DataBit").toString());
    setPlc_Parity(configIni.value("PLC_Parity").toString());
    setPlc_Stopbit(configIni.value("PLC_StopBit").toString());
    configIni.endGroup();

    configIni.beginGroup("Printer_Parameters");
    setPrinter_Port(configIni.value("Printer_Port").toString());
    setPrinter_Baudrate(configIni.value("Printer_BaudRate").toString());
    setPrinter_Databit(configIni.value("Printer_DataBit").toString());
    setPrinter_Parity(configIni.value("Printer_Parity").toString());
    setPrinter_Stopbit(configIni.value("Printer_StopBit").toString());
    configIni.endGroup();

    configIni.beginGroup("Scanner_Parameters");
    setScanner_Port(configIni.value("Scanner_Port").toString());
    setScanner_Baudrate(configIni.value("Scanner_BaudRate").toString());
    setScanner_Databit(configIni.value("Scanner_DataBit").toString());
    setScanner_Parity(configIni.value("Scanner_Parity").toString());
    setScanner_Stopbit(configIni.value("Scanner_StopBit").toString());
    configIni.endGroup();

    configIni.beginGroup("Language");
    setCurrentLanguage(configIni.value("Language_Number").toInt());
    configIni.endGroup();

    configIni.beginGroup("Printer");
    setCurrentPrinter(configIni.value("Printer_Number").toInt());
    configIni.endGroup();

    configIni.beginGroup("Scanner");
    setCurrent_QR_Code(configIni.value("QR_Code_Type").toInt());
    configIni.endGroup();

}

void LocalConfig::save_Data()
{
    // qDebug()<<"Cpp:保存函数触发!!";
    // qDebug()<<"当前将保存的参数: ";

    // qDebug()<<"PLC 端口号:"<<plc_Port();
    // qDebug()<<"PLC 波特率:"<<plc_Baudrate();
    // qDebug()<<"PLC 数据位:"<<plc_Databit();
    // qDebug()<<"PLC 校验位:"<<plc_Parity();
    // qDebug()<<"PLC 停止位:"<<plc_Stopbit();

    // qDebug()<<"打印机 端口号:"<<printer_Port();
    // qDebug()<<"打印机 波特率:"<<printer_Baudrate();
    // qDebug()<<"打印机 数据位:"<<printer_Databit();
    // qDebug()<<"打印机 校验位:"<<printer_Parity();
    // qDebug()<<"打印机 停止位:"<<printer_Stopbit();

    // qDebug()<<"扫码枪 端口号:"<<scanner_Port();
    // qDebug()<<"扫码枪 波特率:"<<scanner_Baudrate();
    // qDebug()<<"扫码枪 数据位:"<<scanner_Databit();
    // qDebug()<<"扫码枪 校验位:"<<scanner_Parity();
    // qDebug()<<"扫码枪 停止位:"<<scanner_Stopbit();

    QString currentDir = QCoreApplication::applicationDirPath();
    // qDebug()<<"当前程序位置: "<<currentDir;

    QString fileName = QString("%1/config.ini").arg(currentDir);
    QSettings configIni(fileName, QSettings::IniFormat);

    configIni.beginGroup("PLC_Parameters");
    configIni.setValue("PLC_Port", plc_Port());
    configIni.setValue("PLC_BaudRate", plc_Baudrate());
    configIni.setValue("PLC_DataBit", plc_Databit());
    configIni.setValue("PLC_Parity", plc_Parity());
    configIni.setValue("PLC_StopBit", plc_Stopbit());
    configIni.endGroup();

    configIni.beginGroup("Printer_Parameters");
    configIni.setValue("Printer_Port", printer_Port());
    configIni.setValue("Printer_BaudRate", printer_Baudrate());
    configIni.setValue("Printer_DataBit", printer_Databit());
    configIni.setValue("Printer_Parity", printer_Parity());
    configIni.setValue("Printer_StopBit", printer_Stopbit());
    configIni.endGroup();

    configIni.beginGroup("Scanner_Parameters");
    configIni.setValue("Scanner_Port", scanner_Port());
    configIni.setValue("Scanner_BaudRate", scanner_Baudrate());
    configIni.setValue("Scanner_DataBit", scanner_Databit());
    configIni.setValue("Scanner_Parity", scanner_Parity());
    configIni.setValue("Scanner_StopBit", scanner_Stopbit());
    configIni.endGroup();

    configIni.beginGroup("Language");
    configIni.setValue("Language_Number", CurrentLanguage());
    configIni.endGroup();

    configIni.beginGroup("Printer");
    configIni.setValue("Printer_Number", CurrentPrinter());
    configIni.endGroup();

    configIni.beginGroup("Scanner");
    configIni.setValue("QR_Code_Type", Current_QR_Code());
    configIni.endGroup();
}

LocalConfig::LocalConfig(QObject *parent)
    : QObject{parent}
{
    init_Data();
}

LocalConfig::~LocalConfig()
{
    save_Data();
}

QString LocalConfig::plc_Port() const
{
    return m_plc_Port;
}

void LocalConfig::setPlc_Port(const QString &newPlc_Port)
{
    if (m_plc_Port == newPlc_Port)
        return;
    m_plc_Port = newPlc_Port;
    emit plc_PortChanged();
}

QString LocalConfig::plc_Baudrate() const
{
    return m_plc_Baudrate;
}

void LocalConfig::setPlc_Baudrate(const QString &newPlc_Baudrate)
{
    if (m_plc_Baudrate == newPlc_Baudrate)
        return;
    m_plc_Baudrate = newPlc_Baudrate;
    emit plc_BaudrateChanged();
}

QString LocalConfig::plc_Databit() const
{
    return m_plc_Databit;
}

void LocalConfig::setPlc_Databit(const QString &newPlc_Databit)
{
    if (m_plc_Databit == newPlc_Databit)
        return;
    m_plc_Databit = newPlc_Databit;
    emit plc_DatabitChanged();
}

QString LocalConfig::plc_Parity() const
{
    return m_plc_Parity;
}

void LocalConfig::setPlc_Parity(const QString &newPlc_Parity)
{
    if (m_plc_Parity == newPlc_Parity)
        return;
    m_plc_Parity = newPlc_Parity;
    emit plc_ParityChanged();
}

QString LocalConfig::plc_Stopbit() const
{
    return m_plc_Stopbit;
}

void LocalConfig::setPlc_Stopbit(const QString &newPlc_Stopbit)
{
    if (m_plc_Stopbit == newPlc_Stopbit)
        return;
    m_plc_Stopbit = newPlc_Stopbit;
    emit plc_StopbitChanged();
}

QString LocalConfig::printer_Port() const
{
    return m_printer_Port;
}

void LocalConfig::setPrinter_Port(const QString &newPrinter_Port)
{
    if (m_printer_Port == newPrinter_Port)
        return;
    m_printer_Port = newPrinter_Port;
    emit printer_PortChanged();
}

QString LocalConfig::printer_Baudrate() const
{
    return m_printer_Baudrate;
}

void LocalConfig::setPrinter_Baudrate(const QString &newPrinter_Baudrate)
{
    if (m_printer_Baudrate == newPrinter_Baudrate)
        return;
    m_printer_Baudrate = newPrinter_Baudrate;
    emit printer_BaudrateChanged();
}

QString LocalConfig::printer_Databit() const
{
    return m_printer_Databit;
}

void LocalConfig::setPrinter_Databit(const QString &newPrinter_Databit)
{
    if (m_printer_Databit == newPrinter_Databit)
        return;
    m_printer_Databit = newPrinter_Databit;
    emit printer_DatabitChanged();
}

QString LocalConfig::printer_Parity() const
{
    return m_printer_Parity;
}

void LocalConfig::setPrinter_Parity(const QString &newPrinter_Parity)
{
    if (m_printer_Parity == newPrinter_Parity)
        return;
    m_printer_Parity = newPrinter_Parity;
    emit printer_ParityChanged();
}

QString LocalConfig::printer_Stopbit() const
{
    return m_printer_Stopbit;
}

void LocalConfig::setPrinter_Stopbit(const QString &newPrinter_Stopbit)
{
    if (m_printer_Stopbit == newPrinter_Stopbit)
        return;
    m_printer_Stopbit = newPrinter_Stopbit;
    emit printer_StopbitChanged();
}

QString LocalConfig::scanner_Port() const
{
    return m_scanner_Port;
}

void LocalConfig::setScanner_Port(const QString &newScanner_Port)
{
    if (m_scanner_Port == newScanner_Port)
        return;
    m_scanner_Port = newScanner_Port;
    emit scanner_PortChanged();
}

QString LocalConfig::scanner_Baudrate() const
{
    return m_scanner_Baudrate;
}

void LocalConfig::setScanner_Baudrate(const QString &newScanner_Baudrate)
{
    if (m_scanner_Baudrate == newScanner_Baudrate)
        return;
    m_scanner_Baudrate = newScanner_Baudrate;
    emit scanner_BaudrateChanged();
}

QString LocalConfig::scanner_Databit() const
{
    return m_scanner_Databit;
}

void LocalConfig::setScanner_Databit(const QString &newScanner_Databit)
{
    if (m_scanner_Databit == newScanner_Databit)
        return;
    m_scanner_Databit = newScanner_Databit;
    emit scanner_DatabitChanged();
}

QString LocalConfig::scanner_Parity() const
{
    return m_scanner_Parity;
}

void LocalConfig::setScanner_Parity(const QString &newScanner_Parity)
{
    if (m_scanner_Parity == newScanner_Parity)
        return;
    m_scanner_Parity = newScanner_Parity;
    emit scanner_ParityChanged();
}

QString LocalConfig::scanner_Stopbit() const
{
    return m_scanner_Stopbit;
}

void LocalConfig::setScanner_Stopbit(const QString &newScanner_Stopbit)
{
    if (m_scanner_Stopbit == newScanner_Stopbit)
        return;
    m_scanner_Stopbit = newScanner_Stopbit;
    emit scanner_StopbitChanged();
}

int LocalConfig::CurrentLanguage() const
{
    return m_CurrentLanguage;
}

void LocalConfig::setCurrentLanguage(int newCurrentLanguage)
{
    if (m_CurrentLanguage == newCurrentLanguage)
        return;
    m_CurrentLanguage = newCurrentLanguage;
    emit CurrentLanguageChanged();
}

int LocalConfig::CurrentPrinter() const
{
    return m_CurrentPrinter;
}

void LocalConfig::setCurrentPrinter(int newCurrentPrinter)
{
    if (m_CurrentPrinter == newCurrentPrinter)
        return;
    m_CurrentPrinter = newCurrentPrinter;
    emit CurrentPrinterChanged();
}

int LocalConfig::Current_QR_Code() const
{
    return m_Current_QR_Code;
}

void LocalConfig::setCurrent_QR_Code(int newCurrent_QR_Code)
{
    if (m_Current_QR_Code == newCurrent_QR_Code)
        return;
    m_Current_QR_Code = newCurrent_QR_Code;
    emit Current_QR_CodeChanged();
}
